package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.AccountAddDTO;
import org.jsola.contract.dto.AccountUpdateDTO;
import org.jsola.contract.query.AccountQuery;
import org.jsola.contract.service.IAccountService;
import org.jsola.contract.vo.AccountListVO;
import org.jsola.contract.vo.AccountVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 账户信息
 *
 * @author JanusMix
 */
@RestController("contractAccountController")
@RequestMapping(ContractConstants.API_PATH + "/v1/accounts")
public class AccountController implements IAccountControllerAPI {

    @Autowired
    private IAccountService accountService;

    @Override
    @PostMapping
    public Result<AccountVO> save(@Valid @RequestBody AccountAddDTO accountAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增账户信息权限校验

        return Result.success(
                accountService.save(accountAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AccountUpdateDTO accountUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改账户信息权限校验

        int count = accountService.update(accountUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{accountId}")
    public Result<Integer> delete(@PathVariable String accountId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除账户信息权限校验

        int count = accountService.deleteByIds(tokenUser, accountId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] accountIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除账户信息权限校验

        int count = accountService.deleteByIds(tokenUser, accountIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{accountId}")
    public Result<AccountVO> findById(@PathVariable String accountId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看账户信息权限校验

        return Result.success(
            accountService.selectById(accountId)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AccountListVO>> listAll(@Valid AccountQuery accountQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看账户信息权限校验

        return Result.success(
            accountService.select(accountQuery)
        );
    }


    @Override
    @GetMapping
    public Result<Page<AccountListVO>> page(@Valid AccountQuery accountQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            accountService.selectPage(accountQuery)
        );
    }


}

